'''
Creates a standalone executable for the HP CSN Claim Manager,
and the Non-CSN Claim Creator GUI modules, using Pyinstaller.
'''
from os.path import (
    join as os_path_join,
    abspath as os_path_abspath,
    basename as os_path_basename,
    splitext as os_path_splitext
)
from platform import system as platform_system
from pkg_resources import get_distribution as get_pkg_distribution
import PySimpleGUI as sg
import click
from PyInstaller.__main__ import run as run_pyinstaller
import chromedriver_autoinstaller
from warranty_manager.claim.common import get_file_path


# To get the correct separator for the OS, and install the latest
# Chrome Drive on Windows only
OS_NAME = platform_system()

if OS_NAME == 'Windows':
    SEPARATOR = ';'
    chromedriver_autoinstaller.install()
else:
    SEPARATOR = ':'

HP_ICON_PATH = os_path_join(os_path_abspath('.'), 'hp_icon.ico')
DELL_ICON_PATH = os_path_join(os_path_abspath('.'), 'dell_icon.ico')


def create_gui(manufacturer, icon_path, version_num, driver_path=None):
    '''Creates an executable file with no external console.'''
    gui_path = get_file_path('GUI Module')

    if gui_path:
        work_path = os_path_join(os_path_abspath('.'), 'build')
        dist_path = os_path_join(os_path_abspath('.'), 'dist')
        spec_path = os_path_join(os_path_abspath('.'), 'spec')
        license_path = os_path_join(os_path_abspath('.'), f'LICENSE{SEPARATOR}.')

        package_name = f'{manufacturer}_{os_path_splitext( os_path_basename(gui_path) )[0]}_{version_num}'

        if driver_path:
            run_pyinstaller([
                '--name=%s' % package_name,
                '--onefile',
                '--noconsole',
                '--workpath=%s' % work_path,
                '--distpath=%s' % dist_path,
                '--specpath=%s' % spec_path,
                '--add-binary=%s' % driver_path,
                '--add-data=%s' % license_path,
                '--icon=%s' % icon_path,
                gui_path,
            ])
        else:
            run_pyinstaller([
                '--name=%s' % package_name,
                '--onefile',
                '--noconsole',
                '--workpath=%s' % work_path,
                '--distpath=%s' % dist_path,
                '--specpath=%s' % spec_path,
                '--add-data=%s' % license_path,
                '--icon=%s' % icon_path,
                gui_path,
            ])


@click.command()
@click.option(
    '-m', '--manufacturer',
    type=click.Choice(['HP', 'Dell'], case_sensitive=False),
    required=True,
    help='Dell or HP'
)
def create_executable(manufacturer):
    '''
    Prompts the user to manually select the correct version of ChromeDriver,
    for the version of Chromium or Google Chrome that's installed on their operating system.
    '''
    sg.theme('Reddit')

    if manufacturer.lower() == 'dell':
        create_gui(manufacturer, DELL_ICON_PATH, get_pkg_distribution('warranty-manager').version)
    else:
        chromedriver_path = get_file_path('ChromeDriver')

        if chromedriver_path:
            create_gui(
                manufacturer,
                HP_ICON_PATH,
                get_pkg_distribution('warranty-manager').version,
                chromedriver_path + f'{SEPARATOR}.',
            )
